package com.layui.ad.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.layui.ad.annotation.DataSource;
import com.layui.ad.constant.OperLogConstant;
import com.layui.ad.entity.OperLog;
import com.layui.ad.enums.DataSourceEnum;
import com.layui.ad.mapper.OperLogMapper;
import com.layui.ad.query.OperLogQuery;
import com.layui.ad.service.OperLogService;
import com.layui.ad.utils.JsonResult;
import com.layui.ad.utils.StringUtils;
import com.layui.ad.vo.OperLogListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.spel.ast.NullLiteral;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class OperLogServiceImpl extends ServiceImpl<OperLogMapper, OperLog> implements OperLogService {

    @Autowired
    private OperLogMapper operLogMapper;

    @Override
    @DataSource(DataSourceEnum.SYSSOURCE)
    public JsonResult getList(OperLogQuery operLogQuery) {
        QueryWrapper<OperLog> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(operLogQuery.getTitle())) {
            queryWrapper.like("title", operLogQuery.getTitle());
        }

        if (operLogQuery.getBusinessType() != null && operLogQuery.getBusinessType() > 0) {
            queryWrapper.eq("business_type", operLogQuery.getBusinessType());
        }

        if (operLogQuery.getStatus() != null && operLogQuery.getStatus() > 0) {
            queryWrapper.eq("status", operLogQuery.getStatus());
        }

        IPage<OperLog> page = new Page<>(operLogQuery.getPage(), operLogQuery.getLimit());
        IPage<OperLog> data = operLogMapper.selectPage(page, queryWrapper);
        List<OperLog> operLogList = data.getRecords();
        List<OperLogListVo> operLogListVoList = new ArrayList<>();
        if (operLogList != null) {
            operLogList.forEach(item -> {
                OperLogListVo operLogListVo = new OperLogListVo();
                BeanUtils.copyProperties(item, operLogListVo);
                if (item.getBusinessType() != null && item.getBusinessType() > 0) {
                    operLogListVo.setBusinessTypeName(OperLogConstant.OPERLOG_BUSINESS_TYPE.get(item.getBusinessType()));
                }
                if (item.getStatus() != null && item.getStatus() > 0) {
                    operLogListVo.setStatusName(OperLogConstant.OPERLOG_STATUS.get(item.getStatus()));
                }
                operLogListVoList.add(operLogListVo);
            });

        }
        return JsonResult.ok("操作成功", operLogListVoList, data.getTotal());
    }


    @DataSource(DataSourceEnum.SYSSOURCE)
    @Override
    public JsonResult delete(Integer id) {
        if (id != null && id > 0) {
            OperLog operLog = operLogMapper.selectById(id);
            if (operLog != null) {
                Integer num = operLogMapper.deleteById(id);
                if (num > 0) {
                    return JsonResult.ok();
                } else {
                    return JsonResult.fail("删除失败");
                }
            } else {
                return JsonResult.fail("你删除的数据不存在了");
            }
        } else {
            return JsonResult.fail("参数错误");

        }
    }

    @Override
    @DataSource(DataSourceEnum.SYSSOURCE)
    public JsonResult batchDelete(String ids) {
        if (!StringUtils.isEmpty(ids)) {
            String[] idList = ids.split(",");
            List<String> intList = Arrays.asList(idList);
            Integer num = operLogMapper.deleteBatchIds(intList);
            if (num > 0) {
                return JsonResult.ok();
            } else {
               return JsonResult.fail();
            }
        } else {
            return JsonResult.fail("缺少参数");
        }
    }
}
